<template>
  <div class="avue-contail">
    <avue-crud
      :option="option"
      :data="data"
      :table-loading="option.loading"
      :permission="permissionList"
      v-model:page="page"
      @row-save="save"
      @row-del="del"
      @row-update="update"
      @on-load="init"
      @refresh-change="refresh"
      @selection-change="selectChange"
      v-model="form"
    >
      <template #icon-form="scope">
        <iconSelect :icon-list="iconList" v-model="form.icon"></iconSelect>
      </template>
      <template #icon="{ row }">
        <iconShow :icon="row.icon"></iconShow>
      </template>
      <template #menu-left="{ size }">
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="delBatch"
          v-if="!this.validatenull(permission.SysMenu_remove)"
          >批量删除
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="createButton"
          v-if="!this.validatenull(permission.SysMenu_create)"
          >生成按钮
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>
<script>
import {
  treeList,
  save,
  update,
  remove,
  createButton,
} from "@/api/admin/SysMenu.js";
import iconList from "@/config/iconList.js";
import iconSelect from "@/components/iconSelect/index.vue";
import iconShow from "@/components/iconShow/index.vue";
import { mapGetters } from "vuex";

export default {
  components: {
    iconSelect,
    iconShow,
  },
  data() {
    return {
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 1,
      },
      iconList: iconList,
      data: [],
      form: {},
      selectionList: [],
      option: {
        loading: true,
        selection: true,
        height: "auto",
        searchMenuSpan: 4,
        index: true,
        copyBtn: true,
        column: {
          parentId: {
            label: "上级菜单",
            type: "tree",
            hide: true,
            span: 24,
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/admin/sysMenu/menuTree`,
          },
          name: {
            label: "名称",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入名称",
                trigger: "blur",
              },
            ],
          },
          path: {
            label: "组件路径",
          },
          icon: {
            label: "图标",
            component: "iconSelect",
          },
          authCode: {
            label: "权限代码",
            rules: [
              {
                required: true,
                message: "请输入权限代码",
                trigger: "blur",
              },
            ],
          },
          type: {
            label: "类型",
            type: "radio",
            value: 1,
            dicData: [
              { label: "正常", value: 1 },
              { label: "外链", value: 2 },
            ],
          },
          category: {
            label: "分类",
            type: "radio",
            value: "MENU",
            dicData: [
              { label: "菜单", value: "MENU" },
              { label: "按钮", value: "BUTTON" },
            ],
          },
          keepAlive: {
            label: "页面缓存",
            type: "radio",
            value: true,
            hide: true,
            dicData: [
              { label: "是", value: true },
              { label: "否", value: false },
            ],
          },
          sort: {
            label: "排序",
          },
        },
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    selectIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
    permissionList() {
      return {
        addBtn: !this.validatenull(this.permission.SysMenu_add),
        delBtn: !this.validatenull(this.permission.SysMenu_remove),
        editBtn: !this.validatenull(this.permission.SysMenu_edit),
      };
    },
  },
  methods: {
    createButton() {
      if (this.selectIds.length === 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据生成按钮?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return createButton(this.selectIds);
        })
        .then((res) => {
          this.refreshLoad(res);
        });
    },
    init() {
      this.option.loading = true;
      treeList()
        .then((res) => {
          this.data = res.data.data;
          this.option.loading = false;
        })
        .catch(() => {
          this.option.loading = false;
        });
    },
    save(row, done, loading) {
      loading();
      save(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    update(row, index, done, loading) {
      loading();
      update(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then((res) => {
          this.refreshLoad(res);
        });
    },
    delBatch() {
      if (this.selectIds.length === 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.selectIds);
        })
        .then((res) => {
          this.refreshLoad(res);
        });
    },
    refreshLoad(res) {
      this.init();
      this.$message({
        type: "success",
        message: res.data.msg,
      });
    },
    refresh() {
      this.init();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
